//https://leetcode.cn/problems/kth-smallest-element-in-a-bst/

class Solution {
    int count = 0;
    int ret = 0;
public:
    int kthSmallest(TreeNode* root, int k)
    {
        count = k;
        dfs(root);
        return ret;

    }

    void dfs(TreeNode* root)
    {
        if (root == nullptr || count == 0)
            return;

        dfs(root->left);
        if (count == 1) ret = root->val;
        count--;
        dfs(root->right);
    }
};